package com.mt.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.VO.ShowCinemaScheduleVO;
import com.mt.constants.Result;
import com.mt.enums.Status;
import com.mt.exception.GlobalException;
import com.mt.feign.FilmApiProvider;
import com.mt.mapper.CinemaMapper;
import com.mt.pojo.Cinema;
import com.mt.pojo.Film;
import com.mt.service.CinemaService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2020-12-11
 */
@Service
public class CinemaServiceImpl extends ServiceImpl<CinemaMapper, Cinema> implements CinemaService {

    @Resource
    CinemaMapper cinemaMapper;

    @Resource
    FilmApiProvider filmApiProvider;


    @Override
    public boolean addCinema(Cinema cinema) {
        if (validateCinemaParm(cinema) && existCinemaName(cinema)) {
            int insert = cinemaMapper.insert(cinema);
            if (insert <= 0) {
                throw new GlobalException(Status.ERROR);
            }
        }
        return true;
    }

    @Override
    public boolean modifyCinema(Cinema cinema) {
        if (validateCinemaParm(cinema) && existCinemaName(cinema)) {
            int update = cinemaMapper.updateById(cinema);
            if (update <= 0) {
                throw new GlobalException(Status.ERROR);
            }
        }
        return true;
    }

    @Override
    public boolean removeCinema(Cinema cinema) {
        Cinema existCinema = cinemaMapper.selectById(cinema.getId());
        if (existCinema == null) {
            throw new GlobalException(Status.NOT_EXIST_CINEMA);
        }
        if (cinemaMapper.deleteById(cinema) > 0){
            return true;
        }else {
            throw new GlobalException(Status.ERROR);
        }
    }

    @Override
    public List<Cinema> listAllCinema() {
        return cinemaMapper.selectList(null);
    }

    @Override
    public IPage<Cinema> pageCinema(int currentPage, int pageSize, String cinemaName) {
        IPage<Cinema> page = new Page<>(currentPage,pageSize);
        if (StrUtil.isNotBlank(cinemaName)){
            cinemaName = "%"+cinemaName+"%";
        }else {
            cinemaName =null;
        }
        List<Cinema> cinemas = cinemaMapper.pageCinema(page, cinemaName);
        page.setRecords(cinemas);
        return page;
    }

    /**
     * 根据电影查询有上映场次的电影院信息
     * */
    @Override
    public List<ShowCinemaScheduleVO> listCinemaScheduleByFilm(int filmId, String queryTime) {
        if (StrUtil.isBlank(queryTime)) {
            queryTime= DateUtil.now();
        }
        DateTime dateTime = DateUtil.parse(queryTime);
        String date = dateTime.toString("yyyy-MM-dd");
        String time = dateTime.toString("HH:mm:ss");
        return cinemaMapper.listCinemaScheduleByFilm(filmId,date,time);
    }

    @Override
    public List<Cinema> listCinemaByArea(String area) {
        if (StrUtil.isBlank(area)){
            throw new GlobalException(Status.PARAM_ERROR);
        }
        return cinemaMapper.selectList(Wrappers.<Cinema>lambdaQuery().eq(Cinema::getArea, area));
    }

    @Override
    public List<Cinema> searchCinema(String key, String area) {
        List<Cinema> cinemas = CollUtil.newArrayList();
        if (StrUtil.isBlank(key) && StrUtil.isBlank(area)) {
            cinemas = listAllCinema();
        } else {
            cinemas = cinemaMapper.searchCinema(key, area);
        }
        return cinemas;
    }

    private boolean validateCinemaParm(Cinema cinema) {
        if ((!Validator.isMobile(cinema.getPhone())) || Validator.isEmpty(cinema.getAddress()) ||
                Validator.isEmpty(cinema.getCinemaName()) ) {
            //|| Validator.isEmpty(cinema.getArea()) 暂不使用
            throw new GlobalException(Status.PARAM_ERROR);
        }
        return true;
    }

    private boolean existCinemaName(Cinema cinema) {
        Cinema existCinema = cinemaMapper.selectOne(Wrappers.<Cinema>lambdaQuery().eq(Cinema::getCinemaName, cinema.getCinemaName())
                .eq(Cinema::getArea, cinema.getArea()));
        if (existCinema != null) {
            throw new GlobalException(Status.EXIST_CINEMA);
        }
        return true;
    }


    /**
     * 查询正在上映的电影
     * */
    @Override
    public List<Film> getScheduleFilmListByCinemaId(int cinemaId){
        //todo 逻辑完善: 获取电影的列表

        DateTime dateTime = DateUtil.parse(DateUtil.now());
        String date = dateTime.toString("yyyy-MM-dd");
        List<Integer> scheduleFilmList = cinemaMapper.getCinemaScheduleList(cinemaId);
        if (scheduleFilmList.isEmpty()){
            return new ArrayList<Film>();
        }
        Result filmListResult= filmApiProvider.getFilmList(scheduleFilmList);
//        System.out.println(filmListResult.getData());
        List<Film> filmList= (List<Film>) filmListResult.getData();
        return filmList;
    }

}
